# -*- coding: utf-8 -*- 
# @Time : 2022/9/29 07:02 
# @Author : junjie
# @File : project.py


import requests
import time
from common.fun_log import RunLog, case_run_log


class Project(object):

    def __init__(self):
        self.logger = RunLog()

    @case_run_log
    def create(self, username: str, password: str):
        """自动创建pity项目"""
        domain_url = "https://api.pity.fun"
        login_data = {"username": username,"password": password}
        self.logger.o_append("请求登录接口，获取token")
        login_res = requests.post(f"{domain_url}/auth/login", json = login_data, timeout = 10).json()
        token = dict(token = login_res['data']['token'])
        tt = int(time.time())
        self.logger.o_append("组装创建项目参数")
        create_project_data = {"name":f"demo_{str(tt)}",
                               "app":"test","owner":1,
                               "description":f"funlink自动创建的_{str(tt)}","private":False}
        self.logger.o_append("请求创建项目接口")
        requests.post(f"{domain_url}/project/insert", json=create_project_data, timeout = 10, headers = token)
        return dict(project_name = create_project_data['name'])

if __name__ == '__main__':
    data = {"username":"tester","password":"tester"}
    project = Project()
    res = project.create(**data)
    print(res)
